﻿IF EXISTS (
    SELECT
      1
    FROM sysobjects
    WHERE id = OBJECT_ID('bd_proc_vacation_deduction')
      AND type IN ('P', 'PC')
  )
  DROP PROCEDURE bd_proc_vacation_deduction

GO

-- =============================================
-- Author:      YFC
-- Create date: 2020-07-19 22:39:05
-- Database:    [scm_main-hysj2]
-- Description: 假期管理-扣除假期生成明细记录
-- =============================================

CREATE PROCEDURE bd_proc_vacation_deduction
  @userInfo VARCHAR(MAX) = '',
  @vacationType INT,    
  @operateId BIGINT
AS
BEGIN
  SET NOCOUNT ON;

--  DECLARE @settleYear INT
--         ,@settleMonth INT

  SELECT *
  INTO #user_vacation
  FROM OPENJSON(@userInfo)
  WITH (
  orderId BIGINT,
  userSerial BIGINT,
  applyDate DATE,
  htType  INT,
  deduct_before INT,
  deduct_usage INT,
  deduct_remain INT,
  settleYear INT,
  settleMonth INT,
  detailId INT,
  annualType INT,
  annualDiff INT     --年假录入增加和减少的量
  )



  BEGIN TRY
  	BEGIN TRANSACTION

      UPDATE bd_kq_holidays_time SET user_serial = user_serial WHERE user_serial IN(SELECT user_serial FROM bd_view_kq_holidays_time_rowno WHERE row_no = 1)  

      --未扣除前
      UPDATE v
      SET deduct_before =
            CASE
              WHEN @vacationType IN (1, 2, 5, 8) THEN
                ht.vacation_annual
              WHEN @vacationType = 10 THEN
                ht.rest_total
              WHEN @vacationType IN (20, 21, 22) THEN
                ht.vacation_lactational
              ELSE
                0
            END
         ,detailId = ht.id
         ,settleYear = ht.time_year
         ,settleMonth = ht.time_month
      FROM #user_vacation v
      INNER JOIN (
          SELECT
            *
          FROM bd_view_kq_holidays_time_rowno
          WHERE row_no = 1
        ) ht ON ht.user_serial = v.userSerial

      IF ISNULL(@vacationType, 0) = 1
      BEGIN
        --1：年假申请

        --更新剩余年假
        UPDATE #user_vacation SET deduct_remain = deduct_before - deduct_remain, htType = 1

        UPDATE ht
        SET vacation_annual = vacation_annual - v.deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial AND v.detailId = ht.id
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 2
      BEGIN
        --年假录入增加，usage是剩余时间，使用时间要用录入的时间-原来的时间
      	--2：年假录入增加    --85	15	100
        UPDATE #user_vacation SET htType = 2
        UPDATE #user_vacation SET annualType = 1 WHERE deduct_usage > deduct_before
        UPDATE #user_vacation SET deduct_remain = deduct_usage, htType = 2 WHERE annualType = 1
        UPDATE #user_vacation SET deduct_usage = deduct_remain - deduct_before WHERE annualType = 1

        --年假录入增加，usage是剩余时间，使用时间要用原来的时间-录入的时间
        --3：年假录入减少  80	30	50
        UPDATE #user_vacation SET annualType = 2 WHERE deduct_usage < deduct_before
        UPDATE #user_vacation SET deduct_remain = deduct_usage, htType = 3 WHERE annualType = 1
        UPDATE #user_vacation SET deduct_usage = deduct_before - deduct_remain WHERE annualType = 1

        --年假录入是覆盖当前年假
        UPDATE ht
        SET vacation_annual = v.deduct_remain
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial AND v.detailId = ht.id
        WHERE annualType IN (1, 2)
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 5
      BEGIN
        --5：每年年初初始化
        
        UPDATE #user_vacation SET deduct_before = 0, deduct_remain = deduct_usage, htType = 5

        UPDATE ht
        SET vacation_annual = deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial AND v.detailId = ht.id
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 8
      BEGIN
        --8：年假初始化(系统上线初始化)

        UPDATE #user_vacation SET deduct_before = 0, deduct_remain = deduct_usage, htType = 8

        UPDATE ht
        SET vacation_annual = deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial AND v.detailId = ht.id
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 10
      BEGIN
        --10：调休申请
        UPDATE #user_vacation SET htType = 10, deduct_remain = deduct_before - deduct_usage
--        SELECT * FROM #user_vacation
        UPDATE ht
        SET rest_total = ht.rest_total - v.deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial  AND v.detailId = ht.id
 
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 20
      BEGIN
        --扣除授乳假
        UPDATE #user_vacation SET htType = 20, deduct_remain = deduct_before - deduct_usage
        UPDATE ht
        SET vacation_lactational = ht.vacation_lactational - v.deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial  AND v.detailId = ht.id
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 21
      BEGIN
        --21：授乳假修改
        UPDATE ht
        SET vacation_lactational = v.deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial  AND v.detailId = ht.id
      END
      ELSE
      IF ISNULL(@vacationType, 0) = 22
      BEGIN
        --22：授乳假取消
        UPDATE #user_vacation SET deduct_remain = deduct_before + deduct_usage, htType = 22
        UPDATE ht
        SET vacation_lactational = vacation_lactational + v.deduct_usage
        FROM bd_kq_holidays_time ht
        INNER JOIN #user_vacation v ON v.userSerial = ht.user_serial  AND v.detailId = ht.id
      END
    
--假期类型
--1：年假申请
--2：年假录入增加
--3：年假录入减少
--4：年假取消
--5：每年年初初始化
--6：年假清零
--
--8：年假初始化(系统上线初始化)
--
--10：调休申请
--11：调休取消
--12：调休结算
--13：调休结算取消
--
--20：授乳假
--21：授乳假修改
--22：授乳假取消

      --生成明细数据
      INSERT INTO dbo.bd_kq_holidays(user_serial
      , dept_serial
      , dept_name
      , ho_year
      , ho_month
      , ho_date
      , ho_type
      , deduct_before
      , deduct_usage
      , deduct_remain
      , related_id
      , create_id
      , create_time)
      SELECT v.userSerial, u.user_dep, u.dep_name, YEAR(applyDate), MONTH(applyDate), applyDate, htType, 
        deduct_before, deduct_usage, deduct_remain, orderId, @operateId, GETDATE()
      FROM #user_vacation v
      INNER JOIN bd_view_user_all u ON u.user_serial = v.userSerial  		
  	COMMIT TRANSACTION
  END TRY
  BEGIN CATCH
    DECLARE @errStr VARCHAR(MAX) = ERROR_MESSAGE();

    IF XACT_STATE() <> 0
    BEGIN
      ROLLBACK TRANSACTION;
    END;
    RAISERROR (@errStr, 16, 1);
  END CATCH;

  DROP TABLE #user_vacation

  SET NOCOUNT OFF;
END